{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一、案例1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例 1 某修理店只有一个修理工，来修理的顾客到达过程为 Poisson 流，平均 4 人/h；修理时间服从负指数分布，平均需要 6min。试求：（1）修理店空闲的概率；（2）店内恰有 3 个顾客的概率；（3）店内至少有 1 个顾客的概率；（4）在店内的平均顾客数；（5）每位顾客在店内的平均逗留时间；（6）等待服务的平均顾客数；（7）每位顾客平均等待服务时间；（8）顾客在店内等待时间超过 10min 的概率。    \n",
    "本例可看成一个M/M/1/$\\infty$排队问题，其中$\\lambda=4$，$\\mu=\\frac{1}{0.1}=10$，$\\rho=\\frac{\\lambda}{\\mu}=0.4$。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.修理店空闲的概率  $p_{0}=1-\\rho$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-05T19:38:15.186694Z",
     "start_time": "2021-06-05T19:38:15.182694Z"
    }
   },
   "outputs": [],
   "source": [
    "p0 = 1 - 0.4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.店内恰有3个顾客的概率 $p_{3}=p^3(1-\\rho)$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-05T19:38:15.195694Z",
     "start_time": "2021-06-05T19:38:15.190694Z"
    }
   },
   "outputs": [],
   "source": [
    "p3 = 0.4 ** 3 * (1 - 0.4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-05T18:08:52.087942Z",
     "start_time": "2021-06-05T18:08:52.083942Z"
    }
   },
   "source": [
    "3.店内至少有1个顾客的概率$P\\lbrace N{\\ge}1\\rbrace=1-P_{0}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-05T19:38:15.202695Z",
     "start_time": "2021-06-05T19:38:15.197694Z"
    }
   },
   "outputs": [],
   "source": [
    "p1 = 0.4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.在店内的平均顾客数$L_{s}=\\frac{\\rho}{1-\\rho}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-05T19:38:15.210695Z",
     "start_time": "2021-06-05T19:38:15.204695Z"
    }
   },
   "outputs": [],
   "source": [
    "ls = 0.4 / (1 - 0.4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-05T18:35:16.043539Z",
     "start_time": "2021-06-05T18:35:16.039539Z"
    }
   },
   "source": [
    "5.每位顾客在店内的平均逗留时间$W_{s}=\\frac{L_{s}}{\\lambda}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-05T19:38:15.218696Z",
     "start_time": "2021-06-05T19:38:15.212695Z"
    }
   },
   "outputs": [],
   "source": [
    "ws = 0.67 / 4 * 60 # 分钟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-05T18:39:16.714305Z",
     "start_time": "2021-06-05T18:39:16.710305Z"
    }
   },
   "source": [
    "6.等待服务的平均顾客数$L_{q}=L_{s}-\\rho=\\frac{\\rho^2}{1-\\rho}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-05T19:38:15.225696Z",
     "start_time": "2021-06-05T19:38:15.220696Z"
    }
   },
   "outputs": [],
   "source": [
    "lq = (0.4 ** 2) / (1 - 0.4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-05T18:53:08.428876Z",
     "start_time": "2021-06-05T18:53:08.424876Z"
    }
   },
   "source": [
    "7.每位顾客平均等待服务时间$W_{q}=\\frac{L_{q}}{\\lambda}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-05T19:38:15.233697Z",
     "start_time": "2021-06-05T19:38:15.227696Z"
    }
   },
   "outputs": [],
   "source": [
    "wq = 0.267 / 4 * 60"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-05T18:54:38.916052Z",
     "start_time": "2021-06-05T18:54:38.912051Z"
    }
   },
   "source": [
    "8.顾客在店内逗留时间超过10min的概率$P\\{T>10\\}=e^{-t(\\rho-\\lambda)},t{\\ge}0$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-06-05T19:38:15.790728Z",
     "start_time": "2021-06-05T19:38:15.234697Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 0.367879441171442$"
      ],
      "text/plain": [
       "0.367879441171442"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sympy import E\n",
    "pt = E ** (-10 * (1/(10-4) - 1/(60/4)))\n",
    "pt"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:lkm]",
   "language": "python",
   "name": "lkm"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
